// 定义存储键名常量
const STORAGE_KEYS = {
    ACCESS_TOKEN: 'access_token',
    USER_INFO: 'user_info',
    REMEMBER_ME: 'remember_me',
    USER_AUTH: 'user_auth',
    WORK_CONTENT: 'work_content',
};

// 安全获取存储项
const getItem = (key) => {
    try {
        const value = localStorage.getItem(key);
        return value ? JSON.parse(value) : null;
    } catch (error) {
        console.error(`读取 localStorage 失败 [${key}]:`, error);
        return null;
    }
};

// 安全设置存储项
const setItem = (key, value) => {
    try {
        localStorage.setItem(key, JSON.stringify(value));
    } catch (error) {
        console.error(`写入 localStorage 失败 [${key}]:`, error);
    }
};

// 安全移除存储项
const removeItem = (key) => {
    localStorage.removeItem(key);
};

// 清空所有相关存储
const clearAuthStorage = () => {
    removeItem(STORAGE_KEYS.ACCESS_TOKEN);
    removeItem(STORAGE_KEYS.USER_INFO);
    removeItem(STORAGE_KEYS.USER_AUTH);
};

export const storage = {
    keys: STORAGE_KEYS,
    getItem,
    setItem,
    removeItem,
    clearAuthStorage,
};
